﻿using EMoney.IStock.Client.Business;
using EMoney.IStock.Client.Business.Account;
using EMoney.IStock.Client.Business.MC;
using EMoney.IStock.Client.Core;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThinkDev.Logging;

namespace EMoney.IStock.Client.Web.Controllers
{
    public class AccountController : Controller
    {
        //  账号相关
        // GET: /Account/

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Reg()
        {
            new AccountBll().SetSendCookieVal();    //发送验证码的唯一标识

            ViewBag.Sid = Request["sid"];
            //ViewBag.FirstPubKey = EncryptionUtil.GetJsPublicKey().First();
            //ViewBag.LastPubKey = EncryptionUtil.GetJsPublicKey().Last();

            return View();
        }

        /// 手机注册
        /// </summary>
        /// <returns></returns>
        public ActionResult RegMobileBlack()
        {
            new AccountBll().SetSendCookieVal();    //发送验证码的唯一标识

            ViewBag.Sid = Request["sid"];
            //ViewBag.FirstPubKey = EncryptionUtil.GetJsPublicKey().First();
            //ViewBag.LastPubKey = EncryptionUtil.GetJsPublicKey().Last();

            return View();
        }

        /// 手机注册
        /// </summary>
        /// <returns></returns>
        public ActionResult RegMobileWhite()
        {
            new AccountBll().SetSendCookieVal();    //发送验证码的唯一标识

            ViewBag.Sid = Request["sid"];
            //ViewBag.FirstPubKey = EncryptionUtil.GetJsPublicKey().First();
            //ViewBag.LastPubKey = EncryptionUtil.GetJsPublicKey().Last();

            return View();
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <returns></returns>
        public ActionResult Reset()
        {
            new AccountBll().SetSendCookieVal();    //发送验证码的唯一标识

            //ViewBag.FirstPubKey = EncryptionUtil.GetJsPublicKey().First();
            //ViewBag.LastPubKey = EncryptionUtil.GetJsPublicKey().Last();

            return View();
        }

        /// <summary>
        /// 手机版重置密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ResetMobileWhite()
        {
            new AccountBll().SetSendCookieVal();    //发送验证码的唯一标识

            //ViewBag.FirstPubKey = EncryptionUtil.GetJsPublicKey().First();
            //ViewBag.LastPubKey = EncryptionUtil.GetJsPublicKey().Last();

            return View();
        }

        /// <summary>
        /// 手机版重置密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ResetMobileBlack()
        {
            new AccountBll().SetSendCookieVal();    //发送验证码的唯一标识

            //ViewBag.FirstPubKey = EncryptionUtil.GetJsPublicKey().First();
            //ViewBag.LastPubKey = EncryptionUtil.GetJsPublicKey().Last();

            return View();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public string modify()
        {
            string ret = string.Empty;
            string reqAccount = Request["account"];
            string reqCode = Request["code"];
            string reqPwd = Request["pwd"];
            string reqSid = Request["sid"];

            if (!string.IsNullOrEmpty(reqAccount) && !string.IsNullOrEmpty(reqCode) && !string.IsNullOrEmpty(reqPwd))
            {
                try
                {
                    string account = EncryptionUtil.Decrypt(reqAccount);
                    string code = EncryptionUtil.Decrypt(reqCode);
                    string pwd = EncryptionUtil.Decrypt(reqPwd);

                    if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(pwd))
                    {
                        //验证码是否正确
                        string sendCode = SendBll.GetCode(account);

                        if (string.IsNullOrEmpty(sendCode))
                        {
                            ret = "验证码已失效，请重新获取";
                        }
                        else if (code != sendCode)
                        {
                            ret = "验证码错误，请重新输入";
                        }
                        else
                        {
                            string flag = "1";

                            if (account.IndexOf("@") > -1)
                            {
                                flag = "2";
                            }

                            bool isExist = WebApiBll.IsExistUser(account, flag);

                            if (!isExist)    //账号不存在
                            {
                                switch (flag)
                                {
                                    case "1":
                                        ret = "手机号码不存在";
                                        break;
                                    case "2":
                                        ret = "邮箱账号不存在";
                                        break;
                                    default:
                                        break;
                                }

                                ret = "账号不存在";
                            }
                            else
                            {
                                string data = string.Concat("username=", account, "&newpass=", pwd);

                                switch (flag)
                                {
                                    case "1":
                                        data = string.Concat(data, "&type=1");
                                        break;
                                    case "2":
                                        data = string.Concat(data, "&type=2");
                                        break;
                                    default:
                                        break;
                                }

                                string encryptKey = WebApiHelper.CreateEncryptKey(string.Concat(ConfigHelper.WebAPIUrl, "User/api/IStock.ModifyPassword?appid=", ConfigHelper.UserAppId, "&", data), ConfigHelper.UserWebApiKey);
                                string url = string.Concat(ConfigHelper.WebAPIUrl, "/User/api/IStock.ModifyPassword?appid=", ConfigHelper.UserAppId, "&encrypt=", encryptKey);

                                string result = NetHelper.PostData(data, url);

                                if (!string.IsNullOrEmpty(result))
                                {
                                    WebApiJson obj = JsonHelper.ToObject<WebApiJson>(result);

                                    if (obj != null)
                                    {
                                        switch (obj.RetCode)
                                        {
                                            case "0":
                                                ret = obj.RetCode;
                                                break;
                                            default:
                                                ret = obj.RetMsg;
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), "修改密码失败");
                }
            }

            return ret;
        }

        /// <summary>
        /// 注册新用户
        /// </summary>
        /// <returns></returns>
        public string add()
        {
            string ret = string.Empty;
            string reqAccount = Request["account"];
            string reqCode = Request["code"];
            string reqPwd = Request["pwd"];
            string reqSid = Request["sid"];

            if (!string.IsNullOrEmpty(reqAccount) && !string.IsNullOrEmpty(reqCode) && !string.IsNullOrEmpty(reqPwd))
            {
                try
                {
                    string account = EncryptionUtil.Decrypt(reqAccount);
                    string code = EncryptionUtil.Decrypt(reqCode);
                    string pwd = EncryptionUtil.Decrypt(reqPwd);

                    if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(pwd))
                    {
                        //验证码是否正确
                        string sendCode = SendBll.GetCode(account);

                        if (string.IsNullOrEmpty(sendCode))
                        {
                            ret = "验证码已失效，请重新获取";
                        }
                        else if (code != sendCode)
                        {
                            ret = "验证码错误，请重新输入";
                        }
                        else
                        {
                            //账号是否存在
                            string flag = "1";

                            if (account.IndexOf("@") > -1)
                            {
                                flag = "2";
                            }

                            bool isExist = WebApiBll.IsExistUser(account, flag);

                            if (isExist)    //账号已存在
                            {
                                switch (flag)
                                {
                                    case "1":
                                        ret = "手机号码已存在";
                                        break;
                                    case "2":
                                        ret = "邮箱账号已存在";
                                        break;
                                    default:
                                        break;
                                }

                                ret = "账号已存在";
                            }
                            else
                            { 
                                //注册新用户
                                string data = string.Concat("username=", account, "&password=", pwd, "&pid=152&tsid=", reqSid);

                                switch (flag)
                                {
                                    case "1":
                                        data = string.Concat(data, "&mobile=", account);
                                        break;
                                    case "2":
                                        data = string.Concat(data, "&email=", account);
                                        break;
                                    default:
                                        break;
                                }

                                string encryptKey = WebApiHelper.CreateEncryptKey(string.Concat(ConfigHelper.WebAPIUrl, "User/api/IStock.RegUserByMobile?appid=", ConfigHelper.UserAppId, "&", data), ConfigHelper.UserWebApiKey);
                                string url = string.Concat(ConfigHelper.WebAPIUrl, "/User/api/IStock.RegUserByMobile?appid=", ConfigHelper.UserAppId, "&encrypt=", encryptKey);

                                string result = NetHelper.PostData(data, url);

                                if (!string.IsNullOrEmpty(result))
                                {
                                    WebApiRegUserRet obj = JsonHelper.ToObject<WebApiRegUserRet>(result);

                                    if (obj != null)
                                    {
                                        switch (obj.RetCode)
                                        {
                                            case "0":
                                                ret = obj.RetCode;
                                                break;
                                            case "-104200002":
                                                if (obj.RetMsg.IndexOf("|") > -1)
                                                {
                                                    ret = obj.RetMsg.Split('|')[1];
                                                }
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), "注册新用户失败");
                }                
            }

            return ret;
        }

        /// <summary>
        /// 发送手机或短信验证码
        /// </summary>
        /// <returns></returns>
        public string send()
        {
            AccountBll accountBll = new AccountBll();

            string sendCookieVal = accountBll.GetSendCookieVal();   //获取发送验证码的唯一标识

            if (string.IsNullOrEmpty(sendCookieVal))
            {
                return string.Empty;
            }

            string ret = string.Empty;
            string reqAccount = Request["account"];
            string reqType = Request["type"];   //1：注册 2：重置密码

            if (!string.IsNullOrEmpty(reqAccount))
            {
                try
                {
                    reqAccount = EncryptionUtil.Decrypt(reqAccount);

                    if (!string.IsNullOrEmpty(reqAccount))
                    {
                        string flag = "1";

                        if (reqAccount.IndexOf("@") > -1)
                        {
                            flag = "2";
                        }

                        bool isExist = WebApiBll.IsExistUser(reqAccount, flag);

                        switch (reqType)
                        {
                            case "1":   //注册新用户
                                if (isExist)    //账号已存在
                                {
                                    switch (flag)
                                    {
                                        case "1":
                                            return "手机号码已存在";
                                        case "2":
                                            return "邮箱账号已存在";
                                        default:
                                            break;
                                    }

                                    return "账号已存在";
                                }
                                break;
                            case "2":   //重置密码
                                if (!isExist)    //账号不存在
                                {
                                    switch (flag)
                                    {
                                        case "1":
                                            return "手机号码不存在";
                                        case "2":
                                            return "邮箱账号不存在";
                                        default:
                                            break;
                                    }

                                    return "账号不存在";
                                }
                                break;
                            default:
                                break;
                        }                        

                        ret = accountBll.SendCode(reqAccount, flag, sendCookieVal);
                    }
                }
                catch (Exception ex)
                {
                    LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), "发送手机或短信验证码失败");
                }
            }

            return ret;
        }
    }
}
